vimfandomcom-20200223-history
Runtime syntax check for php
Ever wanted to just check your php script to see if it had any syntax errors? Similar to perl -c?. You could always do it by doing php -l, with this little macro, you can do it in your buffer. Just add the following line in your vimrc, and whenever you want to test, press ctrl-b map :!php -l % Comments * plugin implementing this tip * A generalized syntax checker plugin Use :compiler php Which will :set makeprg=php -lq, plus it will set your default error format. Then, check for syntax errors by typing :make % ---- To improve on this great tip, here's how to check PHP syntax without having to save first: map :w !php -l I would prefer to invoke the command via :make. :set makeprg=php\ -l\ % If you also set 'autowrite' to true the file is automatically written when calling :make See also: * * * ---- Setting this: autocmd QuickFixCmdPre make w will auto-save the buffer when :make is invoked. (useful if you don't want autowrite on for other commands) ---- Having this will make Vim jump to the first error occurred during compilation afterwards: set errorformat=%m\ in\ %f\ on\ line\ %l Well I'm using this to test php syntax pressing in normal mode, if there are errors it automatically opens the error window (you should put the following code into a file that is sourced when editing a php file) function! PHPsynCHK() let winnum =winnr() " get current window number silent make -l % cw " open the error window if it contains error " return to the window with cursor set on the line of the first error (if any) execute winnum . "wincmd w" endfunction :setl makeprg=php :set errorformat=%m\ in\ %f\ on\ line\ %l " Map -P to check the file for syntax :noremap :call PHPsynCHK() ---- all of these tips have one drawback...they force you to write the file first. I wanted a check before the write happens. So i modified the things a little. au! BufWriteCmd *.php call PHPsynCHK() if !exists('*PHPsynCHK') function! PHPsynCHK() ccl let winnum = winnr() " get current window number silent execute "%!php -l -f /dev/stdin | sed 's/\\/dev\\/stdin/".bufname("%")."/g' >.vimerr; cat" silent cf .vimerr cw " open the error window if it contains error " return to the window with cursor set on the line of the first error (if any) execute winnum . "wincmd w" silent undo silent cf if 1 len(getqflist()) w endif endfunction endif set errorformat=%m\ in\ %f\ on\ line\ %l I used to do :!php -l %. The previous tip has by far worked the best for me since that. I made a small modification to return the cursor to the previous position if there were no errors; as it was above sent the cursor to the first line when successfully saved. if !exists('*PHPsynCHK') function! PHPsynCHK() ccl let winnum = winnr() " get current window number let linenum = line('.') let colnum = col('.') silent execute "%!php -l -f /dev/stdin | sed 's/\\/dev\\/stdin/".bufname("%")."/g' >.vimerr; cat" silent cf .vimerr cw " open the error window if it contains error " return to the window with cursor set on the line of the first error (if any) execute winnum . "wincmd w" silent undo silent cf if 1 len(getqflist()) w call cursor(linenum, colnum) endif endfunction endif ----------- if you are having problems with make and file paths, try using lmake instead